//
//  TagManageView.m
//  MacDevTest
//
//  Created by lbxia on 2022/9/2.
//  Copyright © 2022 com.lbx. All rights reserved.
//

#import "TagManageView.h"
#import "TagCollectionItem.h"
#import "NSCustomTransparentView.h"

@interface TagManageView()<NSCollectionViewDelegate,NSCollectionViewDataSource>
@property (weak) IBOutlet NSView *topLineView;
@property (weak) IBOutlet NSView *listBackView;
@property (weak) IBOutlet NSView *bottomLineView;
@property (weak) IBOutlet NSButton *cancelButton;
@property (weak) IBOutlet NSButton *sureButton;


//覆盖xib上的cancel按钮功能和位置，由于xib按钮设置达不到UI的要求
@property (nonatomic, strong) NSButton *cancelBtn;

@property (weak) IBOutlet NSCollectionView *collectionView;
@property (nonatomic, strong) NSCollectionViewFlowLayout* flowLayout;
@property (nonatomic, assign) NSInteger curSelected;


@property (nonatomic, strong) NSView *windowBackView;
@property (nonatomic, copy) void (^sureAction)(void);


@end

@implementation TagManageView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}
- (IBAction)sureAction:(id)sender {
    [self closeAction:nil];
    
    if (_sureAction) {
        _sureAction();
    }
}
- (IBAction)cancelAction:(id)sender {
    [self closeAction:nil];
}
- (IBAction)closeAction:(id)sender {
    
    [_windowBackView removeFromSuperview];
    [self removeFromSuperview];
}

//新建按钮事件
- (IBAction)newAction:(id)sender {
    
}


- (id)initWithCoder:(NSCoder *)coder {
    if  (self = [super initWithCoder:coder]) {
        
        [self addSubview:self.topLineView];
        [self addSubview:self.listBackView];
        [self addSubview:self.bottomLineView];
        
        [self addSubview:self.cancelButton];
        [self addSubview:self.sureButton];
          
    }
    
    return self;
}

- (void)setUI {
    
    _topLineView.wantsLayer = YES;
    _topLineView.layer.backgroundColor = [NSColor colorWithRed:233/255. green:236/255. blue:240/255. alpha:1].CGColor;
 
    _listBackView.wantsLayer = YES;
    _listBackView.layer.backgroundColor = NSColor.whiteColor.CGColor;

    _listBackView.layer.cornerRadius = 4;
    
    _listBackView.layer.borderWidth = 1;
    _listBackView.layer.borderColor = [NSColor colorWithRed:233/255. green:236/255. blue:240/255. alpha:1].CGColor;

    _bottomLineView.wantsLayer = YES;
    _bottomLineView.layer.backgroundColor = [NSColor colorWithRed:233/255. green:236/255. blue:240/255. alpha:1].CGColor;
    
    _sureButton.wantsLayer = YES;
    _sureButton.layer.backgroundColor = [NSColor colorWithRed:0. green:120/255. blue:250/255. alpha:1].CGColor;
    _sureButton.layer.cornerRadius = 4;
    
    _cancelButton.wantsLayer = YES;
//    _cancelButton.bordered = NO;
    _cancelButton.layer.backgroundColor = NSColor.whiteColor.CGColor;
    _cancelButton.layer.cornerRadius = 4;
    _cancelButton.layer.borderColor = NSColor.redColor.CGColor;
    _cancelButton.bezelStyle = NSBezelStyleTexturedSquare;


    
    self.flowLayout = [[NSCollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = NSMakeSize(368, 40);
     self.flowLayout.minimumLineSpacing = 0;
     self.flowLayout.minimumInteritemSpacing = 0;
     self.collectionView.collectionViewLayout = self.flowLayout;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
   
    self.collectionView.enclosingScrollView.borderType = NSNoBorder;
    
    [self.collectionView registerClass:[TagCollectionItem class] forItemWithIdentifier:@"TagCollectionItem"];

    //设置了，cell点击才有delegate反馈
    self.collectionView.selectable = YES;
    
    
    //取消代码布局覆盖
    self.cancelBtn = [NSButton buttonWithTitle:@"取消" target:self action:@selector(cancelAction:)];
    _cancelBtn.wantsLayer = YES;
    _cancelBtn.layer.backgroundColor = NSColor.whiteColor.CGColor;
    _cancelBtn.layer.cornerRadius = 4;
    _cancelBtn.bezelStyle = NSBezelStyleTexturedSquare;
    [self setButtonTextColor:_cancelBtn textColor:[NSColor colorWithRed:32/255. green:42/255. blue:48/255. alpha:1]];
    
    [self addSubview:_cancelBtn];
    _cancelBtn.translatesAutoresizingMaskIntoConstraints = NO;
    [_cancelBtn.rightAnchor constraintEqualToAnchor:_cancelButton.rightAnchor].active = YES;
    [_cancelBtn.centerYAnchor constraintEqualToAnchor:_cancelButton.centerYAnchor].active = YES;
    [_cancelBtn.widthAnchor constraintEqualToAnchor:_cancelButton.widthAnchor].active = YES;
    [_cancelBtn.heightAnchor constraintEqualToAnchor:_cancelButton.heightAnchor].active = YES;

    //xib取消按钮隐藏
    _cancelButton.hidden = YES;
}

- (void)setButtonTextColor:(NSButton*)btn textColor:(NSColor*)textColor {
    
    NSMutableAttributedString *attrTitle =
    
    [[NSMutableAttributedString alloc] initWithAttributedString:[btn attributedTitle]];
    
    NSUInteger len = [attrTitle length];
    
    NSRange range = NSMakeRange(0, len);
    
    [attrTitle addAttribute:NSForegroundColorAttributeName value:textColor range:range];
    
    [attrTitle fixAttributesInRange:range];
    
    [btn setAttributedTitle:attrTitle];
}

- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{

    
    TagCollectionItem *item = [collectionView makeItemWithIdentifier:@"TagCollectionItem" forIndexPath:indexPath];
    
        
    item.selected = _curSelected >= 0 && indexPath.item == _curSelected ;
   
    item.row = indexPath.item;
    item.action = ^(NSInteger tag,NSInteger row) {
      
        NSLog(@"choose tag:%ld,row=%ld",tag,row);
    };
    
    return item;
}

//如果未调用，可能底层View拦截了mouseDown事件
- (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
{
    
    for (NSIndexPath *indexpath in indexPaths) {
        
        NSInteger row = indexpath.item;
        NSInteger column = indexpath.section;
        
        NSLog(@"didselect row:%ld,col:%ld",(long)row,(long)column);
        
        self.curSelected = row;
        
        [collectionView reloadData];
        
    }
}


+ (void)showWithSureAction:(void(^)(void))sureAction
{
    
    NSWindow *keywindow = [NSApplication sharedApplication].keyWindow;
    NSView *superView = keywindow.contentView.superview;
    
    if (![superView isKindOfClass:NSClassFromString(@"NSTitlebarContainerView")] ){
        
        keywindow = [NSApplication sharedApplication].mainWindow;
        superView = keywindow.contentView.superview;
    }
    
    NSArray *arrayViews = nil;
    if ( ![[NSBundle mainBundle]loadNibNamed:@"TagManageView" owner:self topLevelObjects:&arrayViews] ) {
        return;
    }
  
    TagManageView *tagView = nil;
    
    for (NSView *view in arrayViews) {
        
        if ( [view isKindOfClass:[TagManageView class]] ) {
            
            tagView = (TagManageView*)view;
            break;
        }
    }
    
    
    NSView *windowBackView = [[NSCustomTransparentView alloc]init];
    windowBackView.wantsLayer = YES;
    windowBackView.layer.backgroundColor = [NSColor colorWithRed:0 green:0 blue:0 alpha:0.4].CGColor;
    windowBackView.frame = superView.frame;
    [superView addSubview:windowBackView];
    
    windowBackView.translatesAutoresizingMaskIntoConstraints = NO;
    [windowBackView.centerYAnchor constraintEqualToAnchor:superView.centerYAnchor].active = YES;
    [windowBackView.centerXAnchor constraintEqualToAnchor:superView.centerXAnchor].active = YES;
    [windowBackView.widthAnchor constraintEqualToAnchor:superView.widthAnchor].active = YES;
    [windowBackView.heightAnchor constraintEqualToAnchor:superView.heightAnchor].active = YES;
    
    tagView.wantsLayer = YES;
    tagView.layer.backgroundColor = NSColor.whiteColor.CGColor;
    tagView.layer.cornerRadius = 3;
    tagView.windowBackView = windowBackView;
    tagView.sureAction = sureAction;
    
    [superView addSubview:tagView];
    
    [tagView setUI];
    
    tagView.translatesAutoresizingMaskIntoConstraints = NO;

    [tagView.centerXAnchor constraintEqualToAnchor:superView.centerXAnchor].active = YES;
    [tagView.centerYAnchor constraintEqualToAnchor:superView.centerYAnchor].active = YES;
    [tagView.widthAnchor constraintEqualToConstant:400].active = YES;
    [tagView.heightAnchor constraintGreaterThanOrEqualToConstant:600].active = YES;
    
}

@end
